package com.iweb.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String path = req.getContextPath() + "/day03/";
        if ("jerry".equals(username) && "123456".equals(password)) {
            // 转发
            RequestDispatcher dispatcher = req.getRequestDispatcher("/day03/welcome.jsp");
            dispatcher.forward(req, resp);
        } else {
            // 重定向
            resp.sendRedirect(path + "login.jsp");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        this.doPost(req, resp);
    }
}
